package com.ofps.web.controller.forms;

import com.ofps.forms.common.utils.R;
import com.ofps.forms.domain.AnswerScore;
import com.ofps.forms.domain.Dto.MyFormDto;
import com.ofps.forms.domain.MyForm;
import com.ofps.forms.mapper.MyUserMapper;
import com.ofps.forms.service.MyFormService;
import com.ofps.forms.service.MyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 表单属
 *
 * @author pyn
 * @email 1661829453@qq.com
 * @date 2022-05-02 20:58:29
 */
@RestController
@RequestMapping("form")
public class MyFormController {
    @Autowired
    private MyFormService myFormService;
    @Autowired
    private MyUserService myUserService;
    @Autowired
    private AnswerBlankController answerBlankController;
    @Autowired
    private AnswerDatetimeController answerDatetimeController;
    @Autowired
    private AnswerListboxController answerListboxController;
    @Autowired
    private AnswerRadioController answerRadioController;
    @Autowired
    private AnswerScoreController answerScoreController;
    @Autowired
    private AnswerSlideController answerSlideController;
    @Autowired
    private  AnswerCheckboxController answerCheckboxController;
    /**
     * 根据用户查询表单列表 返回表单（form）
     * 参数为RecycleBin
     */
    @RequestMapping("/queryFormList")
    @RequiresPermissions("form:queryFormList")
    public R queryFormList( @RequestBody MyFormDto formDto) {
        return myFormService.queryFormList(formDto);
    }

    /**
     * 保存表单
     */
    @RequestMapping("/saveForm")
    @RequiresPermissions("form:saveForm")
    public R saveForm(@RequestBody MyForm myForm) {
        return myFormService.saveForm(myForm);
    }


    /**
     * 根据表单ID删除表单
     **/
    @RequestMapping("/deleteForm")
    @RequiresPermissions("form:deleteFrom")
    public R deleteForm(@RequestBody Map<String, Object> body) {
        return myFormService.deleteFormById(body);
    }


    /**
     * ToDo
     * 提交表单
     */
    @RequestMapping("/submitForm")
    @RequiresPermissions("form:submitForm")
    public R submitForm(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return myFormService.submitForm(request, body);
    }

    /**
     * 查询原始表单（不返回作答信息）
     * 根据表单id 查询表单信息和问题、选项等信息
     */
    @RequestMapping("/showForm")
    @RequiresPermissions("form:showForm")
    public R showForm(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        return myFormService.showForm(request, body);
    }

    /**
     * 修改表单状态
     */
    @RequestMapping("/editFormStatus")
    @RequiresPermissions("form:editFormStatus")
    public R editFormStatus(@RequestBody Map<String, Object> body) {
        return myFormService.editFormStatus(body);
    }

    /**
     * ToDo
     * 查询作答表单
     * 根据表单id 查询表单信息和问题、选项等信息
     */
    @RequestMapping("/checkResult")
    @RequiresPermissions("form:checkResult")
    public R checkResult(@RequestBody Map<String, Object> body) {
        return myFormService.checkResult(body);
    }


    //TODO:统计问卷填写结果，做图形化展示

    /**
     * 返回表单所有答案
     * 都是 answer
     * 0 text文本题
     * 1 radio单选题
     * 2 checkbox多选题
     * 3 listbox下拉
     * 4 slide滑动条
     * 5 datetime日期时间
     * 1，先根据form_id找question
     * 2，根据question_id找问题的答案
     */
    @RequestMapping("/FormToTalResult")
    @RequiresPermissions("form:FormToTalResult")
    public R FormResult(HttpServletRequest request, @RequestBody Map<String, Object> body) {
        Map<String, Object> TotalAnswer = new HashMap<String, Object>();
        TotalAnswer = myFormService.QueryFormResult(request, body);
        if (null == TotalAnswer)
            return R.ok("暂无信息!");
        else
            return R.ok("查询成功!").put("form", TotalAnswer);
    }

    @RequestMapping("/queryWithId")
    @RequiresPermissions("form:queryWithId")
    public R queryWithId(@RequestBody Map<String, Object> body) {
        List<MyForm> form = new ArrayList<>();
        form = myFormService.querywithId(body.get("id").toString());
        return R.ok("查询成功!").put("form", form);
    }

    @RequestMapping("/updateForm")
    @RequiresPermissions("form:updateForm")
    public R update(@RequestBody MyForm myForm) {
        return myFormService.updateById(myForm) ? R.ok("更新成功") : R.error("更新失败");
    }
    @RequestMapping("/querywithdepartmentid")
    @RequiresPermissions("form:querywithdepartmentid")
    public R queryWithDepartmentId(HttpServletRequest request, @RequestBody Map<String, Object> body){
        //Integer userId = TokenUtil.getId(request);
        List<Object> departmentForm = new ArrayList<>();
        List<MyForm> formEntityList = myFormService.queryWithdepartmentId(body);
        for(MyForm formEntity : formEntityList){
            Map<String,Object> Form = new HashMap<>();
            if(null != formEntity.getFormName())
                Form.put("formName",formEntity.getFormName());
            else
                Form.put("formName",null);
            if(null != formEntity.getDepartmentId())
                Form.put("departmentId",formEntity.getDepartmentId());
            else
                Form.put("departmentId",null);
            if(null != formEntity.getUserId()){
                Form.put("userName",myUserService.queryUserNameWithUserId(formEntity.getUserId().toString()));
                Form.put("nickName",myUserService.queryNickNameWithUserId(formEntity.getUserId().toString()));
            }
            else{
                Form.put("userName",null);
                Form.put("nickName",null);
            }
            if(null != formEntity.getFormInfo())
                Form.put("formInfo",formEntity.getFormInfo());
            else
                Form.put("formInfo",null);
            if(null != formEntity.getFormUrl())
                Form.put("formUrl",formEntity.getFormUrl());
            else
                Form.put("formUrl",null);
            if(null != formEntity.getId())
                Form.put("id",formEntity.getId());
            else
                Form.put("id",null);
            if(null != formEntity.getFormPassword())
                Form.put("formPassword",formEntity.getFormPassword());
            else
                Form.put("formPassword",null);
            if(null != formEntity.getBeginTime())
                Form.put("beginTime",formEntity.getBeginTime().toString());
            else
                Form.put("beginTime",null);
            if(null != formEntity.getEndTime())
                Form.put("endTime",formEntity.getEndTime().toString());
            else
                Form.put("endTime",null);
            if(null != formEntity.getCreateTime())
                Form.put("createTime",formEntity.getCreateTime().toString());
            else
                Form.put("createTime",null);
            if(null != formEntity.getCounts())
                Form.put("formCounts",formEntity.getCounts().toString());
            else
                Form.put("formCounts",null);
            if(null != formEntity.getFormStatus())
                Form.put("formStatus",formEntity.getFormStatus().toString());
            else
                Form.put("formStatus",null);
            if(null != formEntity.getIpLimit())
                Form.put("ipLimit",formEntity.getIpLimit().toString());
            else
                Form.put("ipLimit",null);
            if(null != formEntity.getIsAnonymous())
                Form.put("isAnonymous",formEntity.getIsAnonymous().toString());
            else
                Form.put("isAnonymous",null);
            if(null != formEntity.getTimeLimit())
                Form.put("timeLimit",formEntity.getTimeLimit().toString());
            else
                Form.put("timeLimit",null);
            if(null != formEntity.getTags())
                Form.put("tags",formEntity.getTags().toString());
            else
                Form.put("tags",null);
            departmentForm.add(Form);
        }
        if (null == departmentForm)
            return R.ok("暂无信息!");
        else
            return R.ok("查询成功!").put("departmentForm",departmentForm);
    }

    @RequestMapping("/summerizeall")
    @RequiresPermissions("form:summerizeall")
    public  R queryall(@RequestBody Map<String, Object> body){
        Integer question_type = Integer.parseInt(body.get("question_type").toString());
        switch (question_type){
            case 0:
                return answerBlankController.summerize(body);
            case 1:
                return answerRadioController.summerize(body);

            case 2:
                return answerCheckboxController.summerize(body);

            case 3:
                return answerListboxController.summerize(body);

            case 4:
                return answerSlideController.summerize(body);
            case 5:
                return answerDatetimeController.summerize(body);
        }
            return R.ok("暂无信息!");
    }
}
